var search_interval_handler=null;
var search_interval=450;
var house_region=null;
var house_price=null;
var house_type=null;
var house_category=null;
var page_num=1;
var page_size=20;
var s_keyword="";
var s_region="0";
var s_region_type="1";
var s_price="0";
var s_type="0";
var s_category="0";
var pid=getParameter("pid");//渠道商
var rid=getParameter("rid");//首页传来的一级区域
var selectRegionType = getParameter("regionType");//首页传来的区域类型

var hkeyword=getParameter("hkeyword");
var user=getUserEntityFromCache();


getSign();
$(function () {
    init();
    addEvent();
    if (!user.isLogin()) {//没登录
        //微信授权回调回来
        if(code!=undefined&&code!=null&&code.length>0&&state=="pocket"){
            getWxUserInfo();
        }
        else {
            user.clearUserInfoCache();
            getUserAuthorize();
        }
    }
    else {
        if(user.mobile==null||user.mobile==undefined||user.mobile==""){
            window.location.href="/user/mobilebind";
        }
        getHouseData();
    }
})

function getHouseData() {
    //getHouseList();
    //getAdvs();
    
    getBasicData();
}

function init() {
    if(rid&&rid!=undefined&&rid.length>0)
    {
        s_region_type="0";//一级分类
        s_region=rid;
        
        console.log('selectRegionType='+selectRegionType+'rid='+rid);
        //首页直接选中了区域
	    if(selectRegionType && selectRegionType.length>0){
	    	s_region_type = selectRegionType;
	    }
    }
    
    

    // 周期检查搜索框的值是否有变化，有变化则进行自动联想
    if (search_interval_handler != null) {
        clearInterval(search_interval_handler);
    }

    search_interval_handler = setInterval(function() {
        if($(".search_cancel").css("display")!="none"){
            var value = ($("input[type=search]").val() || "").trim();
            if(value && value.length > 0) {
                $("input[type=search]").change();
            } else {
                $("input[type=search]").val("").change();
            }
            doOnChange(value);
        }
    }, search_interval);
}

function configSwiper() {
    var swiper = new Swiper('.swiper-container', {
        slidesPerView: 1,
        spaceBetween: 0,
        loop: true,
        autoplay: {
            delay: 2800,
            disableOnInteraction: false,
        },
        pagination: {
            el: '.swiper-pagination',
            clickable: true,
        },
    });
}

function resetSearch() {
    s_keyword="";
    s_region_type="2";//一级分类
    s_region="0";//默认展示上海
    s_category="0";
    s_price="0";
    s_type="0";
    $(".choose_price span").text("价格");
    $(".choose_type span").text("类型");
    $(".choose_flat span").text("户型");
    $(".choose_area span").text("区域");
}
function addEvent() {
    //事件订阅
    $.subscribe("_scroll_touch_bottom",getHouseList);
    $.subscribe("_data_basic_loaded",handlerBasicData);

    $(window).scroll(function() {
        var scrollTop = $(this).scrollTop();
        var scrollHeight = $(document).height();
        var windowHeight = $(this).height();
        if (scrollTop + windowHeight == scrollHeight) {
            // 此处是滚动条到底部时候触发的事件，在这里写要加载的数据，或者是拉动滚动条的操作
            if($(".more_page").attr("data-status")=="0"){
                $.publish("_scroll_touch_bottom");
            }
        }
    });

    $("input[type=search]").change(function () {
        doOnChange($(this).val());
    });

    $("#search_keywords").click(function () {
        $(".search_block").addClass("search_block_active");
    });

    $("#search_keywords").blur(function () {
        //if($(".search_cancel").text()=="搜索"){
            //resetSearch();
            s_keyword=$.trim($(this).val());
            if(s_keyword.length>0){
                handlerHouseSearch();
            }

        //}

        $(".search_block").removeClass("search_block_active");
    });

    $(".search_cancel").click(function () {
        if($(this).text()=="搜索"){
            resetSearch();
            s_keyword=$.trim($(this).val());
            handlerHouseSearch();
        }
        else {
            $(this).val("");
        }
        $(".search_block").removeClass("search_block_active");
    });
	
	document.onkeyup = function (e) {
//		console.log('aabbcc');
		var code = e.charCode || e.keyCode;
		if (code == 13) {
			s_keyword=$.trim($("#search_keywords").val());
            if(s_keyword.length>0){
            	
            	handlerHouseSearch();
            	document.activeElement.blur();
            	
            }
		}
	}
	
    $("#banners").on("click",".swiper-slide",function () {
        window.location.href="/house/detail?hid="+$(this).attr("data-id");
    });

    $(".ad_close").click(function () {
    		
    		console.log('im close1');
    	
            $(".mask").fadeOut(400,function () {
                $(this).css("z-index",10000);
            });
            $(".pop_ad").fadeOut(400);
        }
    );

    $(".choose_bar li").click(function () {
        $(".choose_bar li").find("a").removeClass("on");
        $(this).find("a").toggleClass("on");
    });

    //类型点击事件
    $(".choose_type").click(function () {
        toggleHouseTypePanel();
    });

    $("#choose_block").on("click",".choose_house_type li",function () {
        $(".choose_type span").text($(this).attr("data-text"));
        s_type=$(this).attr("data-id");
        handlerHouseSearch();
        toggleHouseTypePanel();
    });

    //户型点击事件
    $(".choose_flat").click(function () {
        toggleHouseFlatPanel();
    });

    $("#choose_block").on("click",".choose_house_flat li",function () {
        $(".choose_flat span").text($(this).attr("data-text"));
        s_category=$(this).attr("data-id");
        handlerHouseSearch();
        toggleHouseFlatPanel();
        
    });

    //价格点击事件
    $(".choose_price").click(function () {
        toggleHousePricePanel();
    });

    $("#choose_block").on("click",".choose_house_price li",function () {
        $(".choose_price span").text($(this).attr("data-text"));
        s_price=$(this).attr("data-id");
        handlerHouseSearch();
        toggleHousePricePanel();
    });

    //区域点击事件
    $(".choose_area").click(function () {
        toggleHouseAreaPanel();
    });

    $("#choose_block").on("click",".ui-area-right li",function () {
    	if($(this).attr("data-text")!='不限'){
        	$(".choose_area span").text($(this).attr("data-text"));
        }
    	
        if($(this).attr("data-id")=="0"){
            s_region_type="0";//一级分类
            s_region=$(this).attr("data-city");            
        }
        else {
            s_region_type="1";//二级分类
            s_region=$(this).attr("data-id");           
        }
        handlerHouseSearch();
        getAdvsByCity();
        toggleHouseAreaPanel();
    });

    $(".choose_house_area").on("click",".ui-area-left li",function () {
    	
    	console.log('aa bb cc');
        $(".ui-area-left li").removeClass("on");
        $(this).addClass("on");
        $(".ui-area-right li").first().attr("data-city",$(this).attr("data-id"));
		
		var regid = $(this).attr("data-id");
		changecityName(regid);
		
        var child_str=$(this).attr("data-child");
        if(child_str!=null&&child_str!=undefined&&child_str.toString()!="undefined"&&child_str.length>0){
            child_str=child_str.replace(/'/g,"\"");
            
        }
        else {
            child_str="[]";
        }
		
        reloadChildRegion($.parseJSON(child_str),$(this).attr("data-id"));

        if(parseInt($(this).attr("data-id"))<=0){
            s_region="0";
            handlerHouseSearch();
            
            toggleHouseAreaPanel();
        }


    });
	
	function changecityName(regid){		
		var regioninfo = getRegionByid(regid);
		if(regioninfo){
			$('#houseareaname').text(regioninfo.region_name);
		}
		else{
			$('#houseareaname').text('区域');
		}
	}
	
    $(".mask").on("click",function () {
    	
    	console.log('im close2');
    	
        if($("pop_ad").css("display")=="block"){
            $(".mask").fadeOut(400,function () {
                $(this).css("z-index",10000);
            });
            $(".pop_ad").fadeOut(400);
        }
        else {
            $("body").removeClass("pop_modal");
            $(".search_block").removeClass("search_block_fixed");
            $(".choose_bar").removeClass("choose_bar_fixed");
            $(this).hide();
            $(".choose_panel").hide();
            $(".choose_bar li").find("a").removeClass("on");
        }
    });

    $(".more_page").click(function () {
        if($(this).attr("data-status")=="0"){//可以点击加载的模式
            getHouseList();
        }
    });

    $("#house_list").on("click",".house_item",function () {
        window.location.href="/house/detail?hid="+$(this).attr("data-id")+"&region_id="+$(this).attr("data-region");
    });

    $("#house_list").on("click",".house_return",function () {
        window.location.href="https://mp.weixin.qq.com/s?__biz=MzUxMDUwNDk5OQ==&mid=2247483738&idx=1&sn=8d42401efe80d71f1fa4ad928bfcd9c0&chksm=f900b812ce773104296101798aded368bb54da81d3761b36bd79355a24b13aae90cce77a9f86#rd";
        e.preventDefault();
    });

}

//楼盘类型pannel显示与关闭
function toggleHouseTypePanel() {
    if($(".choose_house_type").css("display")=="none"){
        $(".mask").fadeIn(300,function () {
            $("body").addClass("pop_modal");
        });
        $(".search_block").addClass("search_block_fixed").fadeIn(300);
        $(".choose_panel").hide();
        $(".choose_bar").addClass("choose_bar_fixed").fadeIn(300);
        $(".choose_house_type").fadeIn(300);
    }
    else {
        $(".search_block").removeClass("search_block_fixed");
        $(".choose_bar li").find("a").removeClass("on");
        $(".choose_bar").removeClass("choose_bar_fixed");
        $(".choose_house_type").fadeOut(300);
        $(".mask").fadeOut(300,function () {
            $("body").removeClass("pop_modal");
        });
    }
}

//楼盘户型pannel显示与关闭
function toggleHouseFlatPanel() {
    if($(".choose_house_flat").css("display")=="none"){
        $(".mask").fadeIn(300,function () {
            $("body").addClass("pop_modal");
        });
        $(".search_block").addClass("search_block_fixed").fadeIn(300);
        $(".choose_panel").hide();
        $(".choose_bar").addClass("choose_bar_fixed").fadeIn(300);
        $(".choose_house_flat").fadeIn(300);
    }
    else {
        $(".choose_bar li").find("a").removeClass("on");
        $(".search_block").removeClass("search_block_fixed");
        $(".choose_bar").removeClass("choose_bar_fixed");
        $(".choose_house_flat").fadeOut(300);
        $(".mask").fadeOut(300,function () {
            $("body").removeClass("pop_modal");
        });
    }
}

//楼盘价格pannel显示与关闭
function toggleHousePricePanel() {
    if($(".choose_house_price").css("display")=="none"){
        $(".mask").fadeIn(300,function () {
            $("body").addClass("pop_modal");
        });
        $(".choose_panel").hide();
        $(".search_block").addClass("search_block_fixed").fadeIn(300);
        $(".choose_bar").addClass("choose_bar_fixed").fadeIn(300);
        $(".choose_house_price").fadeIn(300);
    }
    else {
        $(".choose_bar li").find("a").removeClass("on");
        $(".search_block").removeClass("search_block_fixed");
        $(".choose_bar").removeClass("choose_bar_fixed");
        $(".choose_house_price").fadeOut(300);
        $(".mask").fadeOut(300,function () {
            $("body").removeClass("pop_modal");
        });
    }
}

//楼盘区域pannel显示与关闭
function toggleHouseAreaPanel() {
    if($(".choose_house_area").css("display")=="none"){

        $(".mask").fadeIn(300,function () {
            $("body").addClass("pop_modal");
        });
        $(".choose_panel").hide();
        $(".search_block").addClass("search_block_fixed").fadeIn(300);
        $(".choose_bar").addClass("choose_bar_fixed").fadeIn(300);
        $(".choose_house_area").fadeIn(300);
    }
    else {
        $(".choose_bar li").find("a").removeClass("on");
        $(".search_block").removeClass("search_block_fixed");
        $(".choose_bar").removeClass("choose_bar_fixed");
        $(".choose_house_area").fadeOut(300);
        $(".mask").fadeOut(300,function () {
            $("body").removeClass("pop_modal");
        });
    }
}

//加载二级区域
function reloadChildRegion(data,city_id) {
    if(data&&data.length>0){
        var region_child_html='';

        for(var j in data){
            region_child_html+='<li data-id="'+data[j].id+'" id="regiont_'+data[j].id+'" data-text="'+data[j].region_name+'"><a href="javascript:void(0);">'+data[j].region_name+'</a></li>';
        }
        $(".ui-area-right ul li").first().attr("data-city",city_id);

        $(".ui-area-right ul li[data-id!='0']").remove();
        $(".ui-area-right ul").append(region_child_html);
    }
    else {
        $(".ui-area-right ul li[data-id!='0']").remove();
    }
}

function doOnChange(val) {
	
    if ( val && val.length >0) {
        $(".search_cancel").text("搜索");
    } else {
        $(".search_cancel").text("取消");
    }
}

function getBasicData(){
    $.ajax({
        type: "get",
        async: true,
        url: "/v1/basic-data/index",
        data: {},
        dataType: "json",
        beforeSend:function(){

        },
        success: function(data){
            if(data.code=="success"){
                house_region=data.data.regions;
                house_price=data.data.prices;
                house_type=data.data.houseType;
                house_category=data.data.category;
                $.publish("_data_basic_loaded");
                
                
            }
            else {
                showAlert(data.message);
            }
            hideLoading();
        },
        error: function(){
            hideLoading();
        }
    });
}

function handlerBasicData() {
    //绑定价格数据
    if(house_price&&house_price.length>0){
        var price_html="";
        for(var i in house_price){
            price_html+='<li data-id="'+house_price[i].id+'" data-text="'+house_price[i].text+'"><h5>'+house_price[i].text+'</h5></li>';
        }
        $(".choose_house_price ul").append(price_html);
    }

    //绑定户型数据
    if(house_type&&house_type.length>0){
        var type_html="";
        for(var i in house_type){
            type_html+='<li data-id="'+house_type[i].number+'" data-text="'+house_type[i].house_type+'"><h5>'+house_type[i].house_type+'</h5></li>';
        }
        $(".choose_house_flat ul").append(type_html);
    }

    //绑定类型数据
    if(house_category&&house_category.length>0){
        var category_html="";
        for(var i in house_category){
            category_html+='<li data-id="'+house_category[i].id+'" data-text="'+house_category[i].category+'"><h5>'+house_category[i].category+'</h5></li>';
        }
        $(".choose_house_type ul").append(category_html);
    }

    //绑定区域数据
    if(house_region&&house_region.length>0){
        var region_parent_html='';
        var region_child_html='';
        var child_string="";

        for(var i in house_region){
            if(i==0){
                child_string=JSON.stringify(house_region[i].child);
                if(child_string==undefined||child_string.toString()=="undefined"){
                    child_string="";
                }
                child_string=child_string.replace(/\"/g,"'");
                region_parent_html+='<li data-id="'+house_region[i].id+'" id="region_'+house_region[i].id+'" data-child="'+child_string+'" class="on"> <a href="javascript:void(0);">'+house_region[i].region_name+'</a></li>';

                for(var j in house_region[i].child){
                    region_child_html+='<li id="regiont_'+house_region[i].child[j].id+'" data-id="'+house_region[i].child[j].id+'" data-text="'+house_region[i].child[j].region_name+'"><a href="javascript:void(0);">'+house_region[i].child[j].region_name+'</a></li>';
                }
                $(".ui-area-right ul li").first().attr("data-city",house_region[i].id);
            }
            else {
                child_string=JSON.stringify(house_region[i].child);
                if(child_string==undefined||child_string.toString()=="undefined"){
                    child_string="";
                }
                child_string=child_string.replace(/\"/g,"'");
                region_parent_html+='<li data-id="'+house_region[i].id+'" id="region_'+house_region[i].id+'" data-child="'+child_string+'"><a href="javascript:void(0);">'+house_region[i].region_name+'</a></li>';
            }

            if(rid&&rid!=undefined&&rid.length>0&&rid==house_region[i].id)
            {
                $(".choose_area span").text(house_region[i].region_name);
            }
        }
        $(".ui-area-left ul").append(region_parent_html);
        $(".ui-area-right ul").append(region_child_html);

    }

    $.unsubscribe("_data_basic_loaded");
    
    if(!rid || rid<=0){
		CheckRegionId(0);
    }
    else{   	
		CheckRegionId(rid);
    }
    
}

//
function getRegionByid(rid,rtype=0){
	if(rtype == 1){
		for(var i in house_region){
			for(var j in house_region[i].child){
				if(house_region[i].child[j].id == rid){
					return house_region[i];	
				}
			}
		}		
	}
	else{
		for(var i in house_region){
			if(house_region[i].id == rid){
				return house_region[i];	
			}
		}
	}
}


function CheckRegion(cityname){
	
	var curcity = localStorage.getItem("curCity");
	
	if(curcity && cityname !="海外置业"){
		cityname = curcity;
	}
	
	if(hkeyword){
		s_keyword = hkeyword;
		$("#search_keywords").val(s_keyword);
	}
	
	$('#houseareaname').text(cityname);
	
	for(var i in house_region){
		
		if(house_region[i].region_name == cityname || house_region[i].region_name.indexOf(cityname)>=0){
			var cur_region_id = house_region[i].id;
			
			var regionpanl = $('#region_'+cur_region_id);
			
			if(!regionpanl){
				alert('目前还没有该城市房源');				
				return;
			}
			
			
			$(".ui-area-left li").removeClass("on");
	        regionpanl.addClass("on");
	        $(".ui-area-right li").first().attr("data-city",regionpanl.attr("data-id"));
	
	        var child_str = regionpanl.attr("data-child");
	        if(child_str!=null&&child_str!=undefined&&child_str.toString()!="undefined"&&child_str.length>0){
	            child_str=child_str.replace(/'/g,"\"");
	        }
	        else {
	            child_str="[]";
	        }
	
	        reloadChildRegion($.parseJSON(child_str),regionpanl.attr("data-id"));
			
	        s_region_type="0";//一级分类
	        s_region= cur_region_id;//regionpanl.attr("data-city");
	        
        	handlerHouseSearch();
        	getAdvsByCity();
			break;
		}
	}
	
}

function CheckRegionByName(cityname){
	for(var i in house_region){	
		if(house_region[i].region_name == cityname || house_region[i].region_name.indexOf(cityname)>=0)
			return house_region[i];
			
			for(var j in house_region[i].child){			
				if(house_region[i].child[j].region_name == cityname || house_region[i].child[j].region_name.indexOf(cityname)>=0){				
					return house_region[i].child[j];
				}			
			}
	}		
	return null;
}

function CheckRegionId(cityid){	
//	
//	if(curcity && cityname !="海外置业"){
//		cityname = curcity;
//	}
	
	if(hkeyword){
		s_keyword = hkeyword;
		$("#search_keywords").val(s_keyword);
		handlerHouseSearch();
    	getAdvsByCity();
    	return;
	}
	
	//分级类型
	var rtype = '0';
	//城市一级对象
	var firstobject = null;
	//城市二级对象
	var secendobject = null;
	//是否跳出循环
	var isbreak = false;
	//城市名称
	var cityname = '';
	//一级城市编号
	var firstcityId = 1;
	
	var curcityName = localStorage.getItem("curCity");
	//如果 cityid 为0 那么查看是否有定位数据,如果没有定位数据默认用上海
	if(cityid<=0){
		cityid = 1;
		var objs = CheckRegionByName(curcityName);
		if(objs){
			cityid = objs.id; 
		}
	}
	
	
	for(var i in house_region){	
		if(house_region[i].id == cityid){
			rtype = '0';
			firstobject = house_region[i];
			cityname = firstobject.region_name;
			isbreak = true;
		}
		
		if(isbreak)
			break;
		
		for(var j in house_region[i].child){			
			if(house_region[i].child[j].id == cityid){				
				rtype = '1';
				firstobject = house_region[i];
				secendobject = house_region[i].child[j]
				cityname = secendobject.region_name;
				isbreak = true;
				break;
			}			
		}
		
		if(isbreak)
			break;
	}	
	
	if(firstobject == null){
		rtype = '0';
		firstobject = getRegionByid(1);
		cityname = firstobject.region_name;
	}
	
	
	$('#houseareaname').text(cityname);
	
	firstcityId = firstobject.id;
	
	var regionpanl = $('#region_'+firstcityId);		
	$(".ui-area-left li").removeClass("on");
    regionpanl.addClass("on");
    $(".ui-area-right li").first().attr("data-city",regionpanl.attr("data-id"));

    var child_str = regionpanl.attr("data-child");
    if(child_str!=null&&child_str!=undefined&&child_str.toString()!="undefined"&&child_str.length>0){
        child_str=child_str.replace(/'/g,"\"");
    }
    else {
        child_str="[]";
    }

    reloadChildRegion($.parseJSON(child_str),regionpanl.attr("data-id"));
    $(".ui-area-right li").removeClass("on");
    $('#regiont_'+firstcityId).addClass("on");
    
    s_region_type= rtype;//一级分类
	s_region= firstobject.id;
    if(rtype == '1' && secendobject){
    	s_region = secendobject.id
    }
	
	handlerHouseSearch();
    getAdvsByCity();	
}



function handlerHouseSearch() {
    // $("#search_keywords").val("");
    $("#house_list").empty();
    page_num=1;
    getHouseList();
}


function getAdvs(){
    $.ajax({
        type: "get",
        async: true,
        url: "/v1/house/get-adv",
        data: {},
        dataType: "json",
        beforeSend:function(){
        },
        success: function(data){
            if(data.code=="success"){
                var banners=data.data.banners;
                var pop_adv=data.data.adv;

                handlerBanners(banners);
                handlerAdvs(pop_adv);
            }
            else {
                showAlert(data.message);
            }
        },
        error: function(){

        }
    });
}

function getAdvsByCity(){
    $.ajax({
        type: "get",
        async: true,
        url: "/v1/house/get-adv-city",
        data: {region:s_region},
        dataType: "json",
        beforeSend:function(){
        },
        success: function(data){
            if(data.code=="success"){
                var banners=data.data.banners;
                //var pop_adv=data.data.adv;

                handlerBanners(banners);
                //handlerAdvs(pop_adv);
            }
            else {
                showAlert(data.message);
            }
        },
        error: function(){

        }
    });
}

function handlerAdvs(data) {
    if(data&&data.adv&&data.adv!=undefined&&data.adv.length>0){
        $(".ad_img").attr("src",m_domain+data.adv).parent("a").attr("href","/house/detail?hid="+data.id);
        if (window.localStorage) {
            var show_time=0;
            var now_time=getTimeStamp();
            var show_time_value=localStorage.getItem("show_time");
            if(show_time_value!=null&&show_time_value!=undefined&&show_time_value.toString().length>0){
                show_time=parseInt(show_time_value);
            }

            if(now_time-show_time>6*60*60){
                setTimeout(function () {
                    localStorage.setItem("show_time",now_time);
                    $(".mask").css("z-index",10002).fadeIn(400);
                    $(".pop_ad").fadeIn(500);
                },400);

            }

        }
    }
}

function handlerBanners(data) {
    if(data&&data.length>0){
        var banner_html="";
        for (var i=0;i<data.length;i++){
        	banner_html+='<div class="swiper-slide" data-id="'+data[i].id+'"><img src="'+m_domain+data[i].banner+'" width="100%" /></div>';       	
        }
        $("#banners").html(banner_html);
        $(".swiper-container").show();
        configSwiper();
    }
    else {
        $(".swiper-container").hide();
    }
}


function getHouseList(){
    $.ajax({
        type: "get",
        async: true,
        url: "/v1/house/list",
        data: {page:page_num, pageSize:page_size,region : s_region, price : s_price, room :s_category, type :s_type , name : s_keyword,region_type:s_region_type},
        dataType: "json",
        beforeSend:function(){
            if(parseInt(page_num)<=1){
                showLoading();
            }
            $(".more_page").attr("data-status","1").html("正在加载中...");
        },
        success: function(data){
            if(data.code=="success"){
                var data_list=data.data.list;
                appendHouseData(data_list,data.data.nextPage);
            }
            else {
                showAlert(data.message);
            }
            hideLoading();
        },
        error: function(){
            hideLoading();
        }
    });
}

function appendHouseData(list,next_page) {
    if(list!=null&&list.length>0){
        var house_html="";
        var item;
        var house_room="";
        var house_unit_price="";
        var total_price="";


        for(var i=0;i<list.length;i++){
            item=list[i];
            if(item.room2==null||item.room2==undefined||item.room2.length<=0||item.room2.toString()=="0"){
                house_room=item.room1;
            }
            else {
                house_room=item.room1+"-"+item.room2;
            }

            if(item.house_unit_price==null||item.house_unit_price==undefined||item.house_unit_price.length<=0||item.house_unit_price.toString()=="0"){
                house_unit_price="待定";
            }
            else {
                house_unit_price=item.house_unit_price+"元/㎡";
            }

            if(item.total_price==null||item.total_price==undefined||item.total_price.length<=0||item.total_price.toString()=="0"){
                total_price="待定";
            }
            else {
                total_price=item.total_price+"万起";
            }

            var marks_html="";
            var w = window.screen.width;
            for(var index in item.marks){
                if((w<=320&&index<=3)||(w>320&&index<=4)){
                    marks_html+='<li>'+item.marks[index]+'</li>';
                }
            }

            if(marks_html.length>0){
                marks_html+='<div class="clearfix"></div>';
            }
            house_html+=' <li class="house_item list-unstyled" data-id="'+item.id+'" data-region="'+item.region_id+'" >\n' +
                '            <div class="house_info" style="padding-bottom:'+(marks_html.length?14:0)+'px;">\n' +
                '                <div class="house_img"><img src="'+m_domain+item.img_path+'" class="img-responsive" onerror="this.src=\'/images/house_default.png\'" /></div>\n' +
                '                <div class="house_summary">\n' +
                '                    <p class="house_name">'+item.house_name+'</p>\n' +
                '                    <p class="house_area">'+item.house_area+'㎡ | '+house_room+'室</p>\n' +
                '                    <p class="house_unit_price">单价：'+house_unit_price+'</p>\n' +
                '                    <p class="house_total_price">总价：'+total_price+'</p>\n' +
                '                    <ul class="house_marks">'+marks_html+'</ul>\n' +
                '                </div>\n' +
                '                <div class="house_return">\n' +
                '                    <div class="return_img"><img src="/images/logo_small.png" /></div>\n' +
                '                    <div class="return_des">\n' +
                '                        <p>口袋返现</p>\n' +
                '                        <p class="return_price">'+item.return_money+'万/套</p>\n' +
                '                    </div>\n' +
                '                    <div class="clearfix"></div>\n' +
                '                </div>\n' +
                '                <div class="clearfix"></div>\n' +
                '            </div>\n' +
                '            <div class="house_address">地址：'+item.address+'</div>\n' +
                '        </li>';
        }

        if(house_html!=null&&house_html.length>0){
            $("#house_list").append(house_html);
        }
    }

    //分页处理
    if(parseInt(next_page)>0){
        $(".more_page").attr("data-status","0").html("点击加载更多");
        page_num=next_page;
    }
    else {
        if(parseInt(page_num)<=1&&list.length<=0){
            $(".more_page").attr("data-status","2").html("暂无楼盘数据");
        }
        else {
            $(".more_page").attr("data-status","3").html("没有更多楼盘了");
        }

    }
}


function getSign(){
    $.ajax({
        type: "get",
        url: "/v1/wx-user/get-sign",
        data: {noncestr:noncestr, timestamp:timeStamp,url:window.location.href},
        dataType: "json",
        beforeSend:function(){

        },
        success: function(data){
            if(data.code=="success") {
                if (data.data != null && data.data.sign != null && data.data.sign != undefined) {
                    initWXConfig(data.data.sign);
                }
            }
            else {
                alert(data.err_msg);
            }
        },
        error: function(){
        }
    });
}


function initWXConfig(signature){
    wx.config({
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
        appId: appId, // 必填，公众号的唯一标识，aa3ecfe3da55ba39e817d6f19f70725d
        timestamp: timeStamp, // 必填，生成签名的时间戳
        nonceStr: noncestr, // 必填，生成签名的随机串
        signature: signature,// 必填，签名，见附录1
        jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
    });

    wx.ready(function(){
        initWXShare();
        // config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。
    });

    wx.error(function(res){
        // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
    });

    wx.checkJsApi({
        jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
        success: function(res) {
            // 以键值对的形式返回，可用的api值true，不可用为false
            // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
        }
    });

}

function initWXShare() {
    var share_title="";
    var share_des="";
    var share_link="";
    var share_icon="";

    share_title=house_share_text;
    share_des="";
    share_link=window.location.href;
    share_icon="http://pocket.all-star.top/images/logo_small.jpg";

    wx.onMenuShareAppMessage({
        title: share_title, // 分享标题
        desc: share_des, // 分享描述
        link: share_link,
        imgUrl: share_icon,
        type: '', // 分享类型,music、video或link，不填默认为link
        dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
        success: function () {
            // 用户确认分享后执行的回调函数
        },
        cancel: function () {
            // 用户取消分享后执行的回调函数
        }
    });

    wx.onMenuShareTimeline({
        title: share_title, // 分享标题
        link:share_link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: share_icon, // 分享图标
        success: function () {
            // 用户确认分享后执行的回调函数
        },
        cancel: function () {
            // 用户取消分享后执行的回调函数
        }
    });
}


function getWxUserInfo() {
    getUserInfo(function (data) {
        if(data&&data.openid){
            user=new User(data.openid,data.mobile,data.nickname,data.headimgurl,data.uid,data.role,data.referee,data.parent_code,data.redpacket_money,data.redpacket_num,data.isgetredpacket);
            user.cacheUserInfo(user.openid,user.nickname,user.mobile,user.avatar,user.uid,data.role,data.referee,data.parent_code,data.redpacket_money,data.redpacket_num,data.isgetredpacket);
            getHouseData();
            if(user.mobile==null||user.mobile==undefined||user.mobile==""){
                window.location.href="/user/mobilebind";
            }
            else {
                getHouseData();
            }
        }
        else {
            showAlert(data);
        }
    });
}

